package org.example.window;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.example.data.WaterSensor;
import org.example.function.WaterSensorMapFunction;

public class WindowProcessDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //env.enableCheckpointing(2000);

        DataStreamSource<String> source = env.socketTextStream("localhost", 9999);
        SingleOutputStreamOperator<WaterSensor> wsSource = source.map(new WaterSensorMapFunction());

        KeyedStream<WaterSensor, String> sensorKS = wsSource.keyBy(value -> value.getId());

        WindowedStream<WaterSensor, String, TimeWindow> tumblingWindow =
                sensorKS.window(TumblingProcessingTimeWindows.of(Time.seconds(10)));

        //过期写法
//        tumblingWindow.apply(new WindowFunction<WaterSensor, String, String, TimeWindow>() {
//            /**
//             *
//             * @param s 分组KEY
//             * @param window 窗口对象
//             * @param input 存的数据
//             * @param out 采集器
//             * @throws Exception
//             */
//            @Override
//            public void apply(String s, TimeWindow window, Iterable<WaterSensor> input, Collector<String> out) throws Exception {
//
//            }
//        });
        SingleOutputStreamOperator<String> process = tumblingWindow.process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
            /**
             * 全窗口函数，窗口触发时才会做一次计算
             * @param s 分组KEY
             * @param context 上下文
             * @param elements 存储的数据
             * @param out 采集器
             * @throws Exception
             */
            @Override
            public void process(String s, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                long start = context.window().getStart();
                String startDate = DateFormatUtils.format(start, "yyyy-MM-dd HH:mm:ss.SSS");
                long end = context.window().getEnd();
                String endDate = DateFormatUtils.format(end, "yyyy-MM-dd HH:mm:ss.SSS");
                long l = elements.spliterator().estimateSize();
                out.collect("key为：" + s + "的窗口[" + startDate + "," + endDate + "]中一共有" + l + "条数据,elements:" + elements.toString());
            }
        });
        process.print();

        env.execute("Window API DEMO");
    }
}
